package com.orhon.smartcampus.modules.student.mutation;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.orhon.smartcampus.auth.JwtUtils;
import com.orhon.smartcampus.basicComponents.Result;
import com.orhon.smartcampus.modules.base.dao.SchoolsDao;
import com.orhon.smartcampus.modules.base.entity.Schools;
import com.orhon.smartcampus.modules.student.dao.ArrivesDao;
import com.orhon.smartcampus.modules.student.dao.LearninfoDao;
import com.orhon.smartcampus.modules.student.dao.SInformationDao;
import com.orhon.smartcampus.modules.student.entity.Arrives;
import com.orhon.smartcampus.modules.student.entity.Learninfo;
import com.orhon.smartcampus.modules.student.entity.SInformation;
import com.orhon.smartcampus.modules.student.gqlInputModel.GQLStudentLearnInfoInput;
import com.orhon.smartcampus.modules.student.inputMapper.StudentLearnInfoInputMapper;
import com.orhon.smartcampus.modules.user.dao.TokenService;
import com.orhon.smartcampus.modules.user.dao.UsersDao;
import com.orhon.smartcampus.modules.user.entity.Users;

import graphql.schema.DataFetcher;
import graphql.schema.DataFetchingEnvironment;

@Component
public class AddLearninfoMutation implements DataFetcher {



    @Autowired
    LearninfoDao learninfoDao;

    @Autowired
    UsersDao usersDao;

    @Autowired
    SInformationDao sInformationDao;

    @Autowired
    SchoolsDao schoolsDao;

    @Autowired
    ArrivesDao arrivesDao;

    @Autowired
    TokenService tokenService;



    /**
     * 新增/修改学生学籍基本信息
     * editor : ths
     */
    @Override
    public Object get(DataFetchingEnvironment environment) throws Exception {

        //String token = (String) SecurityUtils.getSubject().getPrincipal();
        String token = tokenService.getSystemToken();
        Long schoolId = JwtUtils.getSchoolId(token);
        GQLStudentLearnInfoInput input = StudentLearnInfoInputMapper.MAPPER.mapInput(environment.getArgument("inputData"));
        Learninfo learninfo = null;
        if (input.getId() != null){
            learninfo = learninfoDao.get(Learninfo.class , input.getId());
            learninfo.setUpdatedAt(new Date());
        }
        else{
            learninfo = new Learninfo();
            learninfo.setCreatedAt(new Date());
        }
        if(input.getUser_id() != null) {
            learninfo.setUserUsersByUserId(usersDao.get(Users.class,input.getUser_id()));
        }
        if(input.getStudent_id() != null) {
            learninfo.setStudentSInformationByStudentId(sInformationDao.get(SInformation.class,input.getStudent_id()));
        }
        if(schoolId != null) {
            learninfo.setBaseSchoolsBySchoolId(schoolsDao.get(Schools.class,schoolId));
        }
        if(input.getArrive_id() != null) {
            learninfo.setStudentArrivesByArriveId(arrivesDao.get(Arrives.class, input.getArrive_id()));
        }
        if(input.getAt_school() != null){

            learninfo.setAtSchool(input.getAt_school());
        }
        if(input.getTransaction_type() != null){

            learninfo.setTransactionType(input.getTransaction_type());
        }
        if(input.getMembership_number() != null){

            learninfo.setMembershipNumber(input.getMembership_number());
        }
        if(input.getClass_code() != null){

            learninfo.setClassCode(input.getClass_code());
        }
        if(input.getStart_year() != null){

            learninfo.setStartYear(input.getStart_year());
        }
        if(input.getFinished_year() != null){

            learninfo.setFinishedYear(input.getFinished_year());
        }
        if(input.getAdmission_method() != null){

            learninfo.setAdmissionMethod(input.getAdmission_method());
        }
        if(input.getWay_to_study() != null){

            learninfo.setWayToStudy(input.getWay_to_study());
        }
        if(input.getWay_to_annex() != null){

            learninfo.setWayToAnnex(input.getWay_to_annex());
        }
        if(input.getStudent_from() != null){

            learninfo.setStudentFrom(input.getStudent_from());
        }
        if(input.getConfirmed() != null){

            learninfo.setConfirmed(input.getConfirmed());
        }
        if(input.getConfirmed_by() != null){

            learninfo.setConfirmedBy(input.getConfirmed_by());
        }

        try{
            learninfoDao.saveOrUpdate(learninfo);
        }
        catch (Exception e){
            return Result.error(500,""+e);
        }

        return learninfo;
    }
}

